import { getRandNodeId } from "@/utils/process";
import { defineAsyncComponent } from "vue";
import JobIcon from "@iconify-icons/ep/stamp";
import EndIcon from "@iconify-icons/ep/finished";
import ParallelIcon from "@iconify-icons/ep/operation";
import SerialIcon from "@iconify-icons/ep/share";

const Nodes = import.meta.glob("./node/*.vue");
const NodeConfigs = import.meta.glob("./config/*.vue");

//批量导出所有的component下面的表单组件
export const NodeComponents = {};
Object.keys(Nodes).forEach(key => {
  const name = key.replace(/^.+\/([^/]+)\.vue$/, "$1");
  NodeComponents[name] = defineAsyncComponent(Nodes[key]);
});

export const NodeComponentConfigs = {};
Object.keys(NodeConfigs).forEach(key => {
  const name = key.replace(/^.+\/([^/]+)\.vue$/, "$1");
  NodeComponentConfigs[name] = defineAsyncComponent(NodeConfigs[key]);
});

export function createNodeTask(taskIndex?: number) {
  return {
    code: getRandNodeId(),
    type: "Task",
    name: taskIndex ? "任务" + taskIndex : "默认任务",
    jobType: "0",
    userId: "",
    userList: [],
    roleId: "",
    roleList: [],
    dutyId: "",
    dutyName: "",
    dynamicUserType: "0",
    dynamicUserId: "",
    dynamicUserName: "",
    dynamicUserLeaderLevel: 0,
    dynamicDeptType: "0",
    dynamicDeptId: "",
    dynamicDeptName: "",
    dynamicEmptyMethod: "0",
    dynamicUserKey: "",
    dynamicFunctionId: "",
    dynamicFunctionParams: [],
    dynamicFunctionName: "",
    timeout: 0,
    sort: 1
  };
}

export function createNodeListener() {
  return {
    code: getRandNodeId(),
    listenerId: "",
    listenerName: "",
    methods: "",
    remark: "",
    eventType: "0",
    sort: 1,
    eventParams: [],
    ruleCode: getRandNodeId(),
    groupType: "0",
    groups: [
      {
        groupCode: getRandNodeId(),
        groupItemType: "0",
        conditions: []
      }
    ]
  };
}

export function createSerialItem() {
  return {
    groupCode: getRandNodeId(),
    groupItemType: "0", //组内条件关系
    conditions: []
  };
}

const createGateway = type => {
  return {
    code: getRandNodeId(),
    type: "Gateway",
    name: "网关",
    props: {
      type: type,
      branch: [
        //默认创建俩分支
        branchNode[type].createBranch(1),
        branchNode[type].createBranch()
      ]
    },
    branch: [[], []] //默认要创建2个空分支
  };
};

//定义分支子节点
const branchNode = {
  Serial: {
    name: "串行网关",
    icon: SerialIcon,
    color: "#1BB782",
    //创建自身
    createBranch(i) {
      return {
        code: getRandNodeId(),
        type: "Serial",
        name: i ? "条件" + i : "默认条件",
        props: {
          isDefault: i ? false : true,
          ruleCode: getRandNodeId(),
          groupType: "0", //组关系
          groups: [
            {
              groupCode: getRandNodeId(),
              groupItemType: "0",
              conditions: []
            }
          ]
        }
      };
    },
    create() {
      return createGateway("Serial");
    }
  },
  Parallel: {
    name: "并行网关",
    icon: ParallelIcon,
    color: "#718dff",
    //创建自身
    createBranch(i) {
      return {
        code: getRandNodeId(),
        type: "Parallel",
        name: "并行分支" + (i ? i : 2),
        props: {
          isDefault: true
        }
      };
    },
    create() {
      return createGateway("Parallel");
    }
  }
};

//开始节点
const Start = {
  create() {
    return {
      code: getRandNodeId(),
      type: "Start",
      name: "开始",
      props: {
        autoAudit: "1",
        subDefFlowId: "",
        subDefFlowName: "",
        copyUserList: [], //抄送人列表
        rejectType: "0",
        timeout: 0,
        formMode: "normal",
        sort: 1,
        tasks: [createNodeTask()],
        listeners: [],
        formItemList: []
      }
    };
  }
};

//开始节点
const End = {
  name: "结束",
  icon: EndIcon,
  color: "#cacaca",
  create() {
    return {
      code: getRandNodeId(),
      type: "End",
      name: "结束",
      props: {
        autoAudit: "1",
        formMode: "normal",
        tasks: [createNodeTask()],
        listeners: [],
        formItemList: []
      }
    };
  }
};

//审批节点
const Job = {
  name: "审批",
  icon: JobIcon,
  color: "#EF994F",
  create() {
    return {
      code: getRandNodeId(),
      type: "Job",
      name: "审批",
      props: {
        executeMode: "",
        parallelMode: "",
        subDefFlowId: "", //节点子流程
        subDefFlowName: "",
        copyUserList: [], //抄送人列表
        rejectType: "0", //被驳回后再次提交时
        passSame: "0", //相同审批人自动通过
        autoAudit: "0", //是否自动审批
        timeout: 0,
        sort: 0,
        formMode: "normal",
        tasks: [createNodeTask()],
        listeners: [],
        formItemList: [],
        approveButtons: ["0", "1", "2", "3", "5", "6", "8", "10"]
      }
    };
  }
};

export default {
  //审批节点
  Job: Job,
  Start: Start,
  End: End,
  //注入分支节点定义
  ...branchNode
};

export type NodeAttribute = {
  code: string;
  name: string;
  type: string;
};

export type Rule = {
  ruleCode?: string;
  ruleType: string;
  groupType: string;
  groups: RuleGroup[];
};

export type RuleGroup = {
  code: string;
  groupItemType: string;
  conditions: RuleGroupItem[];
};

export type RuleGroupItem = {
  code: string;
  itemType: string;
  varKey: string;
  varOperator: string;
  varValue: string;
  dynamicExpression: string;
  dynamicFunctionId: string;
  dynamicFunctionName: string;
  dynamicFunctionParams: any[];
};
